import type { PositionType } from '@/hooks/usePosition';
import { defineStore } from 'pinia';
import { useMapEditStore } from './mapEdit';
import { MapTile } from '@/constants';

export interface EditElementInterface {
  execute: (position: PositionType) => void;
}

export const wallEditElement: EditElementInterface = {
  execute({ x, y }) {
    const { map } = useMapEditStore();
    map[y][x] = MapTile.WALL;
  },
};

export const floorEditElement: EditElementInterface = {
  execute({ x, y }) {
    const { map } = useMapEditStore();
    map[y][x] = MapTile.FLOOR;
  },
};

export const cargoEditElement: EditElementInterface = {
  execute({ x, y }) {
    const { map } = useMapEditStore();
    map[y][x] = MapTile.CARGO;
  },
};

export const targetEditElement: EditElementInterface = {
  execute({ x, y }) {
    const { map } = useMapEditStore();
    map[y][x] = MapTile.TARGET;
  },
};

export const playerEditElement: EditElementInterface = {
  execute({ x, y }) {
    const { map } = useMapEditStore();
    map[y][x] = MapTile.PLAYER;
  },
};

export type NameType = 'player' | 'cargo' | 'wall' | 'floor' | 'target';

export const useElementEditStore = defineStore('element-edit', () => {
  let currentSelectedElement: EditElementInterface;

  function getCurrentSelectedElement() {
    return currentSelectedElement;
  }

  function setCurrentSelectedElement(selectedElement: EditElementInterface) {
    currentSelectedElement = selectedElement;
  }

  return {
    getCurrentSelectedElement,
    setCurrentSelectedElement,
  };
});
